//
//  GRGoodCommentCell.m
//  YC_GRice
//
//  Created by luojinrong11 on 2018/7/24.
//  Copyright © 2018年 YeCai. All rights reserved.
//

#import "GROderGoodCommentCell.h"
#import "GRCommentImageCell.h"
#import "PhotoBrowerManager.h"
#import "GROrderManagerAction.h"
#define maxPhotoCount 6
@interface  GROderGoodCommentCell()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UITextViewDelegate,GRStarsViewDelegate, GRCommentImageCellDelegate>

@end

@implementation GROderGoodCommentCell


- (void)awakeFromNib {
    [super awakeFromNib];
    self.startView.isComment = YES;
    self.startView.delegate = self;
    self.storeStarView.isComment = YES;
    self.storeStarView.delegate = self;
    self.imageList.dataSource = self;
    self.imageList.delegate = self;
    [self.imageList registerNib:[UINib nibWithNibName:@"GRCommentImageCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"CommentImageCell"];
    self.commentTextView.delegate = self;
   
}
-(void)textViewDidChange:(UITextView *)textView{
    if (_isRiceOrder) {
        if (_isStoreComment) {
            self.billModel.goodsEvaluate = textView.text;
        }else{
          self.riceModel.goodsEvaluate = textView.text;
        }
    }else{
    self.goodModel.goodsEvaluate = textView.text;
}
    self.placeTexViewLabel.hidden = textView.text.length;
    if (textView.text.length >250) {
     NSString * subStr   =  [textView.text substringWithRange:NSMakeRange(0, 250)];
        textView.text = subStr;
    }
    self.numLabel.text = [NSString stringWithFormat:@"%ld/250",textView.text.length];
}


-(void)selectedStarScore:(CGFloat)start{
    if (_isRiceOrder) {
        if (_isStoreComment) {
            self.billModel.goodsStarGrade = start;
            if (_delegate && [_delegate respondsToSelector:@selector(selectedStarScore:)]) {
                [_delegate selectedStarScore:start];
            }
        }else{
        self.riceModel.goodsStarGrade = start;
        }
    }else{
    self.goodModel.goodsStarGrade = start;
    }
    self.starLabel.text = [GROrderManagerAction getGradeDes:start];
    if (start <= 2) {
        self.starLabel.textColor = HEXCOLOR(0x666666);
    }else{
        self.starLabel.textColor = HEXCOLOR(0xFB842D);
    }
}

-(void)setBillModel:(GRMyBillModel *)billModel{
    _billModel = billModel;
    self.storeNickName.text = billModel.nikeName;
    if (_isStoreComment) {
        self.storeCommentView.hidden = NO;
    }
    self.storeStarView.score = billModel.goodsStarGrade;
    self.commentTextView.text = billModel.goodsEvaluate;
    self.placeTexViewLabel.hidden = self.commentTextView.text.length;
    [self.imageList reloadData];
}

-(void)layoutSubviews{
    [super layoutSubviews];
    CGFloat count = 1.0;
    if (_isRiceOrder) {
        if (_isStoreComment) {
            count = (self.billModel.goodsEvaluateImg.count+1)/3.0;
            
        }else{
            count = (self.riceModel.goodsEvaluateImg.count+1)/3.0;
    
        }
    }else{
        count = (self.goodModel.goodsEvaluateImg.count+1)/3.0;

    }
    
    self.collectionHeight.constant = count >1?(KscreenWidth-32)/3*2:(KscreenWidth-32)/3;
}


-(void)setGoodModel:(GROrderListGoodModel *)goodModel{
    _goodModel = goodModel;
    [self.goodImageView sd_setImageWithURL:[NSURL URLWithString:goodModel.imgUrl] placeholderImage:[UIImage imageNamed:@"placeholderImage"]];
    self.nameLabel.text = goodModel.goodsName;
    self.specLabel.text = goodModel.specDesc;
    self.startView.score = goodModel.goodsStarGrade;
    self.starLabel.text = [GROrderManagerAction getGradeDes:goodModel.goodsStarGrade];
    self.commentTextView.text = goodModel.goodsEvaluate;
      self.placeTexViewLabel.hidden = self.commentTextView.text.length;
    [self.imageList reloadData];
}

-(void)setRiceModel:(GRRiceOrderModel *)riceModel{
    _riceModel = riceModel;
    self.specLabel.hidden = YES;
    [self.goodImageView sd_setImageWithURL:[NSURL URLWithString:riceModel.mainPhoto] placeholderImage:[UIImage imageNamed:@"placeholderImage"]];
    self.nameLabel.text = riceModel.title;
    self.startView.score = riceModel.goodsStarGrade;
        self.starLabel.text = [GROrderManagerAction getGradeDes:riceModel.goodsStarGrade];
    self.commentTextView.text = riceModel.goodsEvaluate;
    self.placeTexViewLabel.hidden = self.commentTextView.text.length;
    [self.imageList reloadData];
}
-(void)deletePhoto:(GRCommentImageCell *)cell{
        NSIndexPath * index = [self.imageList indexPathForCell:cell];
    if (_isRiceOrder) {
        if (_isStoreComment) {
            [self.billModel.goodsEvaluateImg removeObjectAtIndex:index.row];
        }else{
        [self.riceModel.goodsEvaluateImg removeObjectAtIndex:index.row];
        }
    }else{
        [self.goodModel.goodsEvaluateImg removeObjectAtIndex:index.row];
    }
    if (_delegate && [_delegate respondsToSelector:@selector(changeHeigt:)]) {
        [_delegate changeHeigt:self];
    }
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    GRCommentImageCell * commentImageCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CommentImageCell" forIndexPath:indexPath];
    commentImageCell.delegate = self;
    NSUInteger Count = _isRiceOrder?_isStoreComment?self.billModel.goodsEvaluateImg.count: self.riceModel.goodsEvaluateImg.count:self.goodModel.goodsEvaluateImg.count;
    if (indexPath.row ==  Count && Count+1<= maxPhotoCount) {
        commentImageCell.detletBt.hidden = YES;
        [commentImageCell.photoView setImage:[UIImage imageNamed:@"goodDetail_08"]];
    
    }else{
        NSString * url;
        if (_isRiceOrder) {
            if (_isStoreComment) {
                url = self.billModel.goodsEvaluateImg[indexPath.row];
            }else{
               url = self.riceModel.goodsEvaluateImg[indexPath.row];
            }
        }else{
            url = self.goodModel.goodsEvaluateImg[indexPath.row];
        }
        [commentImageCell.photoView sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"placeholderImage"]];
        commentImageCell.detletBt.hidden = NO;
    }
    return commentImageCell;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake((KscreenWidth - 32)/3, (KscreenWidth - 32)/3);
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    WEAKSELF
    [self.commentTextView resignFirstResponder];
    [[PhotoBrowerManager defaultManager]multipleImages:1 ContentMode:PhotoBrowserContentModeAll CurrentVc:self.curetntVc FinishBlock:^(NSArray<UIImage *> *images) {
       NSUInteger Count = _isRiceOrder?_isStoreComment?self.billModel.goodsEvaluateImg.count: self.riceModel.goodsEvaluateImg.count:self.goodModel.goodsEvaluateImg.count;
        if (indexPath.row == Count &&  Count+1<=maxPhotoCount) {
            [[GRUploadManager defaultManager]uploadSingleIMAGE:[images  firstObject]completion:^(NSString *imgURL, BOOL success) {
                if (_isRiceOrder) {
                    if (_isStoreComment) {
                            [self.billModel.goodsEvaluateImg insertObject:imgURL atIndex:0];
                    }else{
                    [self.riceModel.goodsEvaluateImg insertObject:imgURL atIndex:0];
                      }
                }else{
                [self.goodModel.goodsEvaluateImg insertObject:imgURL atIndex:0];
                }
                
                if (_delegate && [_delegate respondsToSelector:@selector(changeHeigt:)]) {
                    [_delegate changeHeigt:weakSelf];
                }
            }];
        }else{
            [[GRUploadManager defaultManager]uploadSingleIMAGE:[images  firstObject]completion:^(NSString *imgURL, BOOL success) {
                if (_isRiceOrder) {
                    if (_isStoreComment) {
                        [self.billModel.goodsEvaluateImg replaceObjectAtIndex:indexPath.row withObject:imgURL];
                    }else{
                    [self.goodModel.goodsEvaluateImg replaceObjectAtIndex:indexPath.row withObject:imgURL];
                    }
                }else{
                [self.goodModel.goodsEvaluateImg replaceObjectAtIndex:indexPath.row withObject:imgURL];
                }
                
                if (_delegate && [_delegate respondsToSelector:@selector(changeHeigt:)]) {
                    [_delegate changeHeigt:weakSelf];
                }
               
            }];
        }
    }];
}

-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    NSUInteger Count = _isRiceOrder?_isStoreComment?self.billModel.goodsEvaluateImg.count: self.riceModel.goodsEvaluateImg.count:self.goodModel.goodsEvaluateImg.count;
      if (Count +1 <= maxPhotoCount) {
            return Count+1;
        }else{
            return maxPhotoCount;
        }

}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
